<?php
namespace app\configs\admin;

use app\admin\controller\Admin;
use think\Db;

use app\common\builder\ZBuilder; // 引入ZBuilder

class Category extends Admin
{
	//类型列表
    public function index(){
		
        $map = $this->getMap();
        
        $data_list = Db::name('category')
        ->alias("a")
        ->where($map)
        ->paginate();
        
        // 分页数据
        $page = $data_list->render();        
        $btn_edit = [
            'title' => '修改油价',
            'icon'  => 'fa fa-fw fa-pencil',
            'href'  => url('edit', ['id' => '__id__'])
        ];

        
        $css = <<<EOF
           <style>
                .table-builder > tbody > tr > td{white-space: normal;}
           </style>
EOF;
        
        $html = <<<EOF
            <p></p>
EOF;
        return ZBuilder::make('table')
        ->setPageTitle('油价管理')
        ->setTableName('category') // 指定数据表名
        ->setPrimaryKey('categoryId')	//设置主键
        ->hideCheckbox()
        ->setSearch(['a.name' => '标题']) // 设置搜索参数
        ->addColumns([ // 批量添加列
            ['name', '标题','text.edit'],
            ['prices', '今日油价','text.edit'],
            ['down_prices', '下浮','text.edit'],
            ['update_time', '修改时间'],
        ])
        ->addColumn('right_button', '操作', 'btn')        
        ->addRightButton('btn_edit',$btn_edit,true) // 批量添加右侧按钮1
        ->addRightButton('delete') // 批量添加右侧按钮1
        
        ->setRowList($data_list) // 设置表格数据
        ->setPages($page) // 设置分页数据
        ->setExtraCss($css)
        ->setExtraHtml($html,'toolbar_top')
        ->fetch();
    }
	

   
    public function edit($id=null){
        if ($id === null) $this->error('缺少参数');
        $info = Db::name('category')->find($id);
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $time=date("Y-m-d H:i:s");
            $data['update_time']=$time;
            $r=Db::name('category')->where("categoryId",$id)->update($data);
            //写入记录数据
            Db::name('category_log')->insert([
                "categoryId"=>$id,
                "create_time"=>$time,
                "prices"=>$data['prices'],
                "down_prices"=>$data['down_prices'],
            ]);
            if($r){
                return $this->success('修改成功', null, ['_close_pop' => 1]);
            }else {
                return $this->error('修改失败', null, ['_close_pop' => 1]);
            }
        }
      
        return ZBuilder::make('form')
        ->setPageTitle('编辑油价') // 设置页面标题
        ->addFormItems([ // 批量添加表单项
            ['hidden', 'categoryId'],
            ['text', 'name', '标题', ''],
            ['number', 'prices', '今日油价'],
            ['number', 'down_prices', '下浮'],
            
        ])
       ->setFormData($info) // 设置表单数据
        //->isAjax(false)
        ->fetch();
        
    }
    
    
  
    
    
    
    
    
    
   
}
?>